<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" :title="getTitle" @ok="handleSubmit" showFooter width="100%">
    <BasicForm @register="registerForm" >
      <template #userName="{ model, field }">
        <Ainput  v-model:value="model[field]" :placeholder="'请选择' + model.label"
                 @success="e => { handleSuccess1(e, model, field) }">
        </Ainput>

      </template>
    </BasicForm>
    
  </BasicDrawer>
</template>
<script lang="ts" setup>
import {ref, computed, unref, toRaw} from 'vue';
import { BasicDrawer, useDrawerInner } from '@/components/Drawer';

import { BasicForm, useForm } from '@/components/Form';
  import { node_user_formSchema } from './menu.data';
  import Ainput from '@/views/bookManage/components/account/Ainput.vue';

  defineOptions({ name: 'AccountModal' });

  const emit = defineEmits(['success', 'register']);

  const isUpdate = ref(true);
  const rowId = ref('');

  const [registerForm, { setFieldsValue, updateSchema, resetFields, validate }] = useForm({
    labelWidth: 130,
    baseColProps: { span: 24 },
    schemas: node_user_formSchema,
    showActionButtonGroup: false,
    actionColOptions: {
      span: 23,
    },
  });

  const dataValue=ref({});
const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    resetFields();
    setDrawerProps({ confirmLoading: false });
    isUpdate.value = !!data?.isUpdate;
    dataValue.value = data.record;
    if (unref(isUpdate)) {
      data.izMatcheManageValue=data.izMatcheManage==1;
      // rowId.value = data.record.id;
      setFieldsValue({
        ...data.record,
      });
    }

  });

  const getTitle = computed(() => (!unref(isUpdate) ? '新增审批人' : '编辑审批人'));

  async function handleSubmit() {
    try {
      const values = await validate();
      setDrawerProps({ confirmLoading: true });
      values.izMatcheManage=values.izMatcheManageValue?1:0;

      // TODO custom api
      // console.log(values);
      for(let k in values) {
        dataValue.value[k] = values[k];
      }
      closeDrawer();
      emit('success', { isUpdate: unref(isUpdate), values:toRaw(dataValue.value) });
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }


  function handleSuccess1(e, model, field) {
    // console.log("_---e:",e)
    model[field] = e.names;
    model.userId = e.ids;
  }
  
  
</script>
